package com.zmn.plat.admin.controller.channel.bail;


import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.plat.admin.controller.PlatBaseController;
import com.zmn.plat.business.interfaces.channel.bail.ChannelBailBService;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.vo.channel.BailVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 渠道保证金基础修改Controller
 *
 * @author jiafei
 * @since 2021-06-16 09:51:40
 */
@Slf4j
@RestController
@RequestMapping("/channel/bail/modify")
public class ChannelBailModifyController extends PlatBaseController {

    @Resource
    private ChannelBailBService channelBailBService;

    /**
     * 描述: 保存
     *
     * @author jiafei
     * @since 2021-06-16 09:51:40
     */
    @SuppressWarnings("Duplicates")
    @RequiresPermissions("/channel/bail/addEdit.action")
    @RequestMapping("/addEdit")
    public ResponseDTO<String> addEdit(@ModelAttribute BailVO bailVO) {

        if (Objects.isNull(bailVO) || Objects.isNull(bailVO.getChannelId())
                || Objects.isNull(bailVO.getAmount()) || Objects.isNull(bailVO.getType())) {
            log.error("ChannelBailModifyController#addEdit参数不能为空：bailVO：{}", bailVO);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }

        String typeStr = Objects.equals(bailVO.getType(), 1) ? "收退款" : (Objects.equals(bailVO.getType(), 2) ? "收退款" :
                "扣除");

        String label = typeStr +"保证金";
        try {
            LoginStaff mcStaff = getMcStaff();
            // 元转分
            int amountFen = NumberUtil.convertMoneyToFen(bailVO.getAmount().toString());
            channelBailBService.inOrOutBail(bailVO.getChannelId(), bailVO.getType(), amountFen, mcStaff.getRealName(),
                    bailVO.getRemark());

            return ResponseDTO.success(String.valueOf(bailVO.getChannelId()), String.format("%s成功！", label));

        } catch (PlatException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            log.error("error:", e);
            return ResponseDTO.fail(String.format("%s失败！", label));
        }
    }

}
